What is @ethereumjs/rlp?
@ethereumjs/rlp is a JavaScript library for encoding and decoding data using Recursive Length Prefix (RLP) encoding, which is a serialization method used in the Ethereum protocol. This package is part of the EthereumJS project and is commonly used for handling Ethereum data structures.
What are @ethereumjs/rlp's main functionalities?
RLP Encoding
This feature allows you to encode data into RLP format. In the code sample, an array containing the strings 'cat' and 'dog' is encoded into RLP.
const rlp = require('@ethereumjs/rlp');
const encoded = rlp.encode(['cat', 'dog']);
console.log(encoded);
RLP Decoding
This feature allows you to decode RLP-encoded data back into its original form. In the code sample, the previously encoded array is decoded back to ['cat', 'dog'].
const rlp = require('@ethereumjs/rlp');
const encoded = rlp.encode(['cat', 'dog']);
const decoded = rlp.decode(encoded);
console.log(decoded);
Other packages similar to @ethereumjs/rlp
rlp
The 'rlp' package is another library for encoding and decoding data using RLP. It is simpler and more lightweight compared to @ethereumjs/rlp, but it offers similar functionality for basic RLP operations.
ethereumjs-util
The 'ethereumjs-util' package provides a variety of utility functions for Ethereum, including RLP encoding and decoding. It is more comprehensive than @ethereumjs/rlp, offering additional utilities for handling Ethereum addresses, signatures, and more.
@ethereumjs/rlp
Installation
To obtain the latest version, simply require the project using npm
:
npm install @ethereumjs/rlp
Install with -g
if you want to use the CLI.
Usage
import assert from 'assert'
import { RLP } from '@ethereumjs/rlp'
const nestedList = [[], [[]], [[], [[]]]]
const encoded = RLP.encode(nestedList)
const decoded = RLP.decode(encoded)
assert.deepStrictEqual(decoded, nestedList, 'decoded output does not match original')
console.log('assert.deepStrictEqual would have thrown if the decoded output did not match')
Browser
With the breaking release round in Summer 2023 we have added hybrid ESM/CJS builds for all our libraries (see section below) and have eliminated many of the caveats which had previously prevented a frictionless browser usage.
It is now easily possible to run a browser build of one of the EthereumJS libraries within a modern browser using the provided ESM build. For a setup example see ./examples/browser.html.
API
RLP.encode(plain)
- RLP encodes an Array
, Uint8Array
or String
and returns a Uint8Array
.
RLP.decode(encoded, [stream=false])
- Decodes an RLP encoded Uint8Array
, Array
or String
and returns a Uint8Array
or NestedUint8Array
. If stream
is enabled, it will just decode the first rlp sequence in the Uint8Array. By default, it would throw an error if there are more bytes in Uint8Array than used by the rlp sequence.
Buffer -> Uint8Array
With the breaking releases from Summer 2023 we have removed all Node.js specific Buffer
usages from our libraries and replace these with Uint8Array representations, which are available both in Node.js and the browser (Buffer
is a subclass of Uint8Array
).
We have converted existing Buffer conversion methods to Uint8Array conversion methods in the @ethereumjs/util bytes
module, see the respective README section for guidance.
BigInt Support
Starting with v4 the usage of BN.js for big numbers has been removed from the library and replaced with the usage of the native JS BigInt data type (introduced in ES2020
).
Please note that number-related API signatures have changed along with this version update and the minimal build target has been updated to ES2020
.
CLI
rlp encode <JSON string>
rlp decode <0x-prefixed hex string>
Examples
rlp encode '5'
-> 0x05
rlp encode '[5]'
-> 0xc105
rlp encode '["cat", "dog"]'
-> 0xc88363617483646f67
rlp decode 0xc88363617483646f67
-> ["cat","dog"]
EthereumJS
See our organizational documentation for an introduction to EthereumJS
as well as information on current standards and best practices. If you want to join for work or carry out improvements on the libraries, please review our contribution guidelines first.
License
MPL-2.0